package cn.liumouren.jzxd.service.impl;

import cn.liumouren.jzxd.common.utils.PageUtils;
import cn.liumouren.jzxd.common.utils.Query;
import cn.liumouren.jzxd.dao.TagDao;
import cn.liumouren.jzxd.entity.Tag;
import cn.liumouren.jzxd.service.TagService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.Map;


@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagDao, Tag> implements TagService {


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Tag> page = this.page(
                new Query<Tag>().getPage(params),
                new QueryWrapper<Tag>()
        );

        return new PageUtils(page);
    }

    @Override
    public Tag getTagByColumn(Map<String, Object> map) {
        QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            queryWrapper.eq(entry.getKey(),entry.getValue());
        }
        Tag tag = baseMapper.selectOne(queryWrapper);
        return tag;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(Tag entity) {
        entity.setId(IdWorker.getIdStr());
        entity.setAddTime(new Date());
        return SqlHelper.retBool(baseMapper.insert(entity));
    }

    @Override
    @Cacheable(value = "tag",key = "#id",cacheManager = "cacheManager1Day")
    public Tag getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(Tag entity) {
        return SqlHelper.retBool(baseMapper.updateById(entity));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return false;
        }
        return SqlHelper.retBool(baseMapper.deleteBatchIds(idList));
    }

}